package com.dragon.web.commons.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson15.JSONObject;
import com.dragon.app.commons.dto.BankcardSaveDTO;
import com.dragon.app.commons.service.BankService;
import com.dragon.app.ykd.dto.BankCardInfosDTO;
import com.dragon.app.ykd.dto.BankCardLoanDTO;
import com.fintech.common.vo.FintechResult;
import com.marvels.common.constants.PayContants;
import com.marvels.common.utils.MarvelsLogUtil;
import com.upay.sdk.FastJsonUtils;

/**
 * 银行或者关于银行卡
 * @author ljs
 *
 */
@Controller
@RequestMapping("${platform.name.code}")
public class BankController {

	

	@Autowired
	private BankService bankService;
	
	
	
	/**
	 * 根据用户Id按照类型获取银行卡的信息<br>
	 * 0-放款卡,1-还款卡,2-信用卡
	 * 
	 * @param shortUrl
	 * @param response
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "bankCard/getBankCardList")
	public FintechResult getBankCardList(@RequestHeader String accessToken, @RequestHeader String idFintechUmUser,
			@RequestBody BankCardInfosDTO bankCardInfosDTO) {
		return bankService.getBankCardList(accessToken, idFintechUmUser, bankCardInfosDTO);
	}
	
	
	
	@ResponseBody
	@RequestMapping(value = "bankCard/bankBindList")
	public FintechResult getBankBindList(@RequestHeader String accessToken, @RequestHeader String idFintechUmUser,
			@RequestBody BankCardInfosDTO bankCardInfosDTO) {
		return bankService.getBankBindList(accessToken, idFintechUmUser, bankCardInfosDTO);
	}
	
	/**
	 * 接收用户填写的的宝付发送给客户的验证
	 * 去宝付那边获取这个卡跟这个商户号对应的协议号
	 * @param bankcardSaveDTO 
	 * @param accessToken
	 * @param idFintechUmUser
	 * @return
	 * @author shan
	 */
	@ResponseBody
	@RequestMapping(value = "bankCard/confrimBindBankCard")
	public FintechResult confrimBindBankCard(@RequestBody BankcardSaveDTO bankcardSaveDTO,
			@RequestHeader(required = false) String accessToken,
			@RequestHeader(required = false) String idFintechUmUser) {
		return bankService.confrimBindBankCard(bankcardSaveDTO, accessToken, idFintechUmUser);
	}
	
	/**
	 * 设置放款卡 还款卡 type 1 还款卡 2 放款卡 id 放款卡的id
	 * 
	 * @param shortUrl
	 * @param response
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "bankCard/loan/{loanId}")
	public FintechResult bankCardLoan(@PathVariable String loanId, @RequestBody BankCardLoanDTO bankCardLoanDTO,
			@RequestHeader String accessToken, @RequestHeader String idFintechUmUser) {
		return bankService.bankCardLoan(loanId, bankCardLoanDTO, accessToken, idFintechUmUser);
	}
	
	
	/**
	 * 根据银行产品信息预绑卡接口发送验证码
	 * 
	 * @param bankcardSaveDTO
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "bank/preBindCard")
	public FintechResult bankPreBindCard(@RequestBody BankcardSaveDTO bankcardSaveDTO, @RequestHeader(required=false) String accessToken, @RequestHeader(required=false) String idFintechUmUser) {
		return bankService.bankPreBindCard(bankcardSaveDTO, accessToken, idFintechUmUser);
	}
	
	@ResponseBody
	@RequestMapping(value = "bank/unbind")
	public FintechResult unbind(@RequestBody BankcardSaveDTO bankcardSaveDTO, @RequestHeader(required=false) String accessToken, @RequestHeader(required=false) String idFintechUmUser) {
		return bankService.unbind(bankcardSaveDTO.getBankCardNum(), accessToken, idFintechUmUser);
	}	
	
	/**
	 * 查询支持银行列表
	 * @param bankcardSaveDTO
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "bank/sustainBankList")
	public FintechResult sustainBankList(@RequestHeader(required=false) String accessToken, @RequestHeader(required=false) String idFintechUmUser) {
		return bankService.sustainBankList(accessToken, idFintechUmUser);
	}
	
	/**
	 * 根据卡号获取卡BIN信息
	 * 
	 * @param bankCardNo
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "bank/queryBnakCardBin/{bankCardNo}")
	public FintechResult queryBnakCardBin(@PathVariable String bankCardNo) {
		return bankService.queryBnakCardBin(bankCardNo);
	}

	/**
	 * 查询绑定成功的银行卡列表 
	 * 优先展示当前渠道的银行卡，如果存在老渠道的也会查出，根据is_support字段区分当前渠道是否支持
	 * @param accessToken
	 * @param idFintechUmUser
	 * @param bankCardInfosDTO
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "bank/queryBankList")
	public FintechResult queryBankList(@RequestHeader String accessToken, @RequestHeader String idFintechUmUser) {
		return bankService.queryBankList(accessToken, idFintechUmUser);
	}
	
	/**
	 * 设置默认卡
	 * @param accessToken
	 * @param idFintechUmUser
	 * @param bankCardLoanDTO
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "bank/setDefaultCard")
	public FintechResult setDefaultCard(@RequestHeader String accessToken, @RequestHeader String idFintechUmUser, @RequestBody BankCardLoanDTO bankCardLoanDTO) {
		return bankService.setDefaultCard(accessToken, idFintechUmUser, bankCardLoanDTO);
	}
	
	/**
	 * 支付通知回调
	 * @param accessToken
	 * @param idFintechUmUser
	 * @param bankCardLoanDTO
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "pay/callback/yzf")
	public FintechResult payYzfCallBack(HttpServletRequest req, HttpServletResponse resp) {
		JSONObject json = null;
		PrintWriter out = null;
		try {
			try {
				json = FastJsonUtils.convert(req.getInputStream());
			} catch (IOException e) {
				MarvelsLogUtil.getInstance().error("易支付支付结果回调通知获取参数失败",e);
				try {
					out =resp.getWriter();
					out.print("FAILD");
					out.flush();
				} catch (IOException ex) {
					MarvelsLogUtil.getInstance().error("易支付支付结果回调通知获取参数失败",ex);
				}
				return null;
			}
	        json.put("encryptKey",req.getHeader("encryptKey"));
	        json.put("merchantId",req.getHeader("merchantId"));
	        Map<String,String> param = new HashMap<String,String>();
	        param.put("param", json.toJSONString());
			bankService.payResultCallback(param, PayContants.PAY_CHANNEL_YZF.toString());
			try {
				out =resp.getWriter();
				out.print("SUCCESS");
				out.flush();
			} catch (IOException e) {
				MarvelsLogUtil.getInstance().error("易支付支付结果回调通知获取参数失败",e);
				return null;
			}
		}finally {
			if(null != out){
				out.close();
			}
		}
		return null;
	}
	
	public static void main(String[] args) {
		System.out.println(JSON.toJSONString( new BankcardSaveDTO(),SerializerFeature.WriteMapNullValue));
	}
}
